package com.zhfi.dataapply.schedule.reader.chaincode;

import com.alibaba.fastjson.JSON;
import com.zhfi.dataapply.entity.TzResponse;
import com.zhfi.dataapply.service.ForwardService;
import com.zhfi.dataapply.service.chaincode.OriginChainCodeListService;
import org.springframework.batch.item.ItemReader;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据chainCode
 */
public abstract class AbstractChainKeyReader<T> implements ItemReader<T> {

    /**
     * 持久化类型
     */
    private Class<T> clazz;

    /**
     * 最初产业链列表读取偏移量
     */
    protected int originChainListSiteNum;

    /**
     * 数据列表读取的偏移量
     */
    protected int dataSiteNum;

    /**
     * 数据列表
     */
    protected List<T> dataList;

    protected List<String> originChainCodeList;

    protected ForwardService forwardService;

    protected OriginChainCodeListService originChainCodeListService;

    public AbstractChainKeyReader(Class<T> clazz, ForwardService forwardService,
                                  OriginChainCodeListService originChainCodeListService) {
        this.clazz = clazz;
        this.originChainListSiteNum = 0;
        this.dataSiteNum = 0;
        this.dataList = Collections.emptyList();
        this.originChainCodeList = Collections.emptyList();
        this.originChainCodeListService = originChainCodeListService;
        this.forwardService = forwardService;
    }

    @Override
    public T read() {
        if (originChainListSiteNum >= originChainCodeList.size()) {
            return null;
        }
        if (CollectionUtils.isEmpty(dataList) || dataSiteNum >= dataList.size()) {
            this.dataSiteNum = 0;
            originChainListSiteNum ++;
            readChainData();
        }
        if (CollectionUtils.isEmpty(dataList)) {
            return read();
        }
        return dataList.get(dataSiteNum ++);
    }

    protected Map<String, Object> getParams(String chainCode) {
        Map<String, Object> params = new HashMap<>();
        params.put("chainCode", chainCode);
        return params;
    }

    private void readChainData() {
        if (originChainListSiteNum >= originChainCodeList.size()) {
            dataList = Collections.emptyList();
            return;
        }
        //获取企业标识companyId
        String chainCode = originChainCodeList.get(originChainListSiteNum);
        TzResponse tzResponse = forwardService.requestForwarder(getUrl(), getParams(chainCode));
        if (null == tzResponse || CollectionUtils.isEmpty(tzResponse.getData())) {
            originChainListSiteNum ++;
            readChainData();
        } else {
            dataList = JSON.parseArray(JSON.toJSONString(tzResponse.getData()), clazz);
        }
    }

    private void resetChainList() {
        originChainCodeList = originChainCodeListService.getOriginChainCodeList();
    }

    public void reset() {
        originChainListSiteNum = 0;
        resetUrl();
        resetChainList();
        readChainData();
    }

    public abstract String getUrl();
    public abstract void resetUrl();

}
